package com.wits.qso.web.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.wits.platform.common.constants.ViewType;
import com.wits.platform.common.constants.WebConstants;
import com.wits.qso.entity.Attachment;
import com.wits.qso.entity.CList;
import com.wits.qso.entity.MaterialD;
import com.wits.qso.service.CListService;
import com.wits.qso.service.MaterialdService;

@Controller
@RequestMapping(value = "qso/clistView")
public class CListViewController
{
	protected static final Logger logger = LoggerFactory.getLogger( CListViewController.class );
	
	@Autowired
	private CListService cListService = null;
	@Autowired
	private MaterialdService materialdService=null;
	
	@RequestMapping(value = "/toList")
	public String toList(Model model) {
		logger.info("{}", cListService.getCListTree());
		model.addAttribute( "cListTree", cListService.getCListTree() );
		
		return "qso/clistView/list";
	}
	
	@RequestMapping(value = "/play")
	public String toView(Model model, @RequestParam String id, @RequestParam String pType) {
		String resUrl=null;
		MaterialD attachment=materialdService.getOne(id);
		String fileName=(attachment!=null&&attachment.getPath()!=null)?attachment.getPath():"";
		model.addAttribute( "id", id);
		model.addAttribute( "pType", pType);
		model.addAttribute( "fileName", fileName);
		if("swf".equals(pType)){
			resUrl="qso/clistView/swfForm";
		}else{
			resUrl="qso/clistView/form";
		}
		return resUrl;
	}
	
	/**
	 * 
	 *方法摘要：文件下载
	 *@param
	 *@return String
	 */
	@RequestMapping(value="/downloadFile")
	public void downloadFile(HttpServletRequest request,HttpServletResponse response,@RequestParam String id){
		response.setCharacterEncoding("utf-8");
		response.setContentType("multipart/form-data");
		String realPath = request.getRealPath("/");
		MaterialD attachment=materialdService.getOne(id);
		response.setHeader("Content-Disposition", "attachment;fileName="+attachment.getPath());
		try {
			String fileName = realPath + "\\" + attachment.getUri();
			File file=new File(fileName);
			System.out.println(file.getAbsolutePath());
			InputStream inputStream=new FileInputStream(file);
			OutputStream os=response.getOutputStream();
			byte[] b=new byte[1024];
			int length;
			while((length=inputStream.read(b))>0){
				os.write(b,0,length);
			}
			inputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
